package com.mjk.manager.usercenter.scrole;


import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.base.work.BaseWork;
import com.mjk.common.base.work.annotation.BeeWork;
import com.mjk.common.core.jdbc.service.idgenerator.IdGenerateType;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.util.ObjectUtils;
import com.mjk.manager.amis.core.model.SysRole;
import com.mjk.manager.usercenter.scrole.dto.ScRoleDto;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.Arrays;

@BeeWork(work = "scrole")
public class ScRoleWorkProxy extends BaseWork {
		@Autowired
		private BeeDBService beeDBService;

		/**
		 * 获取scRole列表信息
         */
		public BeeResponse getScRoleList(ScRoleDto dto){
			BeeQueryWrapper wrapper = new BeeQueryWrapper(new SysRole());
			wrapper.eq(SysRole::getDELETED, false);
			if(ObjectUtils.isNotEmpty(dto.getRoleId())) {
				wrapper.eq(SysRole::getId,dto.getRoleId());
			}
			if(isPage(dto)) {
				wrapper.setPage(dto.getPageStart(),dto.getPageSize());
                return BeeError.BEE_SUC.tranceError(beeDBService.findManyByWrapperWithPage(wrapper));
			}else {
                return BeeError.BEE_SUC.tranceError(beeDBService.findManyByWrapper(wrapper));
			}
		}
		/**
		 * 保存或修改scRole
         */
		public  BeeResponse saveOrUpdateScRole(SysRole model){
			BeeQueryWrapper wrapper = new BeeQueryWrapper(model);
			model.setUPDATED_AT(LocalDateTime.now());
			model.setUPDATED_BY(model.getCreater());
			if(ObjectUtils.isNotEmpty(model.getId())) {
				wrapper.eq(SysRole::getId,model.getId());
				beeDBService.update(wrapper);
			}else {
				model.setId(IdGenerateType.AUTO_SNOWFLAKE.getGenerator().getId(model));
				model.setCREATED_AT(LocalDateTime.now());
				model.setCREATED_BY(model.getCreater());
				model.setDELETED("0");
//				model.setNAME(model.getDESCR());
				beeDBService.save(model);
			}
			return BeeError.BEE_SUC.tranceError();
		}
		/**
		 * 删除scRole
         */
		public  BeeResponse delScRole(SysRole model){
			needParams(model, "id");
			BeeQueryWrapper wrapper = new BeeQueryWrapper(model);
			if(model.getId().contains(",")){
				wrapper.in(SysRole::getId, Arrays.asList(model.getId().split(",")));
			}else {
				wrapper.eq(SysRole::getId, model.getId());
			}
			beeDBService.delete(wrapper);
			return BeeError.BEE_SUC.tranceError();
		}
}